//
//  MyMotionManager.m
//  TuoLuoYi
//
//  Created by Faker on 5/22/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "MyMotionManager.h"

@implementation MyMotionManager

+(MyMotionManager*)sharedInstance
{
    static MyMotionManager *_sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[MyMotionManager alloc] init];
    });
    return _sharedInstance;
}

-(id)init
{
    if(self=[super init])
    {
        self.motionManager = [[CMMotionManager alloc] init];
    }
    return self;
}

-(void)moveView:(UIView *)mView andUpdateInterVal:(float)interVal andScrollXYZ:(NSString *)xyz andIsRight:(BOOL)isRight
{
    if([self.motionManager isAccelerometerAvailable])
    {
        self.motionManager.accelerometerUpdateInterval = interVal; //监测频率
        __weak UIView* weakView = mView;
        
        //开始监测
        [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
            //设备前后两次的夹角
            double roll    = motion.attitude.roll;  //横滚，将物体绕Z轴旋转
            double pitch   = motion.attitude.pitch; //俯仰，将物体绕X轴旋转
            double yaw     = motion.attitude.yaw;   //航向，将物体绕Y轴旋转
           
            if([xyz isEqualToString:@"z"])
            {
                float direction = roll;
                if(!isRight)
                {
                    direction = -roll;
                }
                weakView.layer.transform = CATransform3DMakeRotation(direction, 0, 0, 1);
            }
            
            if([xyz isEqualToString:@"x"])
            {
                float direction = pitch;
                if(!isRight)
                {
                    direction = -pitch;
                }
                weakView.layer.transform = CATransform3DMakeRotation(direction, 1, 0, 0);
            }
            
            if([xyz isEqualToString:@"y"])
            {
                float direction = yaw;
                if(!isRight)
                {
                    direction = -yaw;
                }
                weakView.layer.transform = CATransform3DMakeRotation(direction, 0, 1, 0);
            }


            
        }];
    }

}

-(void)moveView:(UIView *)mView andOutView:(UIView *)outView andUpdateInterVal:(float)interVal andScrollXYZ:(NSString *)xyz andIsRight:(BOOL)isRight
{
    if([self.motionManager isDeviceMotionAvailable])
    {
        self.motionManager.deviceMotionUpdateInterval = interVal; //监测频率
        __weak UIView* weakView = mView;
        __weak UIView* weakView2 = outView;
        
        //开始监测
        [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
            //设备前后两次的夹角
            double roll    = motion.attitude.roll;  //横滚，将物体绕Z轴旋转
            double pitch   = motion.attitude.pitch; //俯仰，将物体绕X轴旋转
            double yaw     = motion.attitude.yaw;   //航向，将物体绕Y轴旋转
            
            if([xyz isEqualToString:@"z"])
            {
                float direction = roll;
                if(!isRight)
                {
                    direction = -roll;
                }
                weakView.layer.transform = CATransform3DMakeRotation(direction*0.7, 0, 0, 1);
                weakView2.layer.transform = CATransform3DMakeRotation(-direction*0.7, 0, 0, 1);
            }
            
            if([xyz isEqualToString:@"x"])
            {
                float direction = pitch;
                if(!isRight)
                {
                    direction = -pitch;
                }
                weakView.layer.transform = CATransform3DMakeRotation(direction, 1, 0, 0);
            }
            
            if([xyz isEqualToString:@"y"])
            {
                float direction = yaw;
                if(!isRight)
                {
                    direction = -yaw;
                }
                weakView.layer.transform = CATransform3DMakeRotation(direction, 0, 1, 0);
            }
            
            
            
        }];
    }

}

-(void)stopUpdate
{
    if ([self.motionManager isDeviceMotionActive] == YES)
    {
        [self.motionManager stopDeviceMotionUpdates];
    }

}

@end
